package com.sg.business.commons.labelprovider.complex;

import org.eclipse.ui.forms.IFormPart;

import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Folder;
import com.sg.business.model.WorkDeliverableDefinition;
import com.sg.business.model.WorkDocumentDefinition;
import com.sg.widgets.part.editor.fields.AbstractFieldPart;

public class WorkDeliDefFolderPathLabelProvider extends FolderPathLabelProvider {

	@Override
	public String getText(Object element) {
		Folder folder = getFolder(element);
		if (folder != null) {
			return folder.getPath(-1);
		}
		return "";
	}

	private Folder getFolder(Object element) {
		if (element instanceof PrimaryObject) {
			WorkDeliverableDefinition workDeliDef = (WorkDeliverableDefinition) element;
			WorkDocumentDefinition workDocDef = workDeliDef
					.getWorkDocumentDefinition();
			if (workDocDef != null) {
				return workDocDef.getFolder();
			}
		}
		return null;
	}

	@Override
	public String getPresentValue(IFormPart part) {
		Object value = ((AbstractFieldPart) part).getValue();
		Folder po = getFolder(value);
		if (po != null) {
			return po.getPath(-1);
		}
		return "";
	}

}
